package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Constant;
import com.example.demo.model.entity.AdModel;
import com.example.demo.model.entity.ProjectModel;
import com.example.demo.service.impl.AdService;
import com.example.demo.service.impl.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = Constant.API_VERSION + "/project")
public class ProjectController {

    @Resource
    ProjectService projectService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ProjectModel save(@RequestBody ProjectModel model) throws Exception {
        if (StringUtils.isEmpty(model.getProjectName()) || StringUtils.isEmpty(model.getProjectImg())) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "请检查项目名称、图片是否正确添加"));
        }
        model.setCreateTime(System.currentTimeMillis());
        model.setProjectId(UUID.randomUUID().toString());
        return projectService.save(model);
    }


    @RequestMapping(value = "/{project_id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable(value = "project_id") String projectId) throws Exception {
        projectService.delete(projectId);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getProjectPage(@RequestParam(value = "page_size", defaultValue = "10") String pageSize, @RequestParam(value = "page_number", defaultValue = "1") String pageNumber,
                                     @RequestParam(value = "shop_id", defaultValue = "") final String shopId,
                                     @RequestParam(value = "keyword", defaultValue = "") final String keyword) throws Exception {
        Page<ProjectModel> adPage = projectService.getProjectPage(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (StringUtils.isNotEmpty(shopId)) {
                    Path<Integer> shopIdPath = root.get("shopId");
                    predicateList.add(criteriaBuilder.equal(shopIdPath, shopId));
                }
                if (StringUtils.isNotEmpty(keyword)) {
                    Path<String> namePath = root.get("projectName");
                    predicateList.add(criteriaBuilder.like(namePath, "%" + keyword + "%"));
                }
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        }, new PageRequest(Integer.valueOf(pageNumber) - 1, Integer.valueOf(pageSize)));

        JSONObject result = new JSONObject();
        result.put("total", adPage.getTotalElements());
        result.put("items", adPage.getContent());
        return result;
    }

}
